﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using static TS_XY_Train_Repaire.ToolSystem.ToolDataMgr;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using System.Timers;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public partial class Tool_System_OrderList_Form : DevExpress.XtraEditors.XtraForm
    {
        private int m_iOrderType = -1;
        private bool m_bRefresh = true;
        private System.Windows.Forms.Timer m_tTimer = new System.Windows.Forms.Timer();//实例化Timer类，设置间隔时间为10000毫秒；

        public Tool_System_OrderList_Form()
        {
            InitializeComponent();

            _LoadGrid();

            m_tTimer.Interval = 10000;
            m_tTimer.Tick += new EventHandler(timer_Tick);
            m_tTimer.Enabled = true;
            //m_tTimer.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件；
            //m_tTimer.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
            //m_tTimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
        }

        public int IOrderType { get => m_iOrderType; set => m_iOrderType = value; }

        private void Tool_System_OrderList_Form_Load(object sender, EventArgs e)
        {
            //if (IOrderType == 0)
            //{
            //    this.Text = "申请工单列表";
            //}
            //else if (IOrderType == 1)
            //{
            //    this.Text = "待领用工单列表";
            //}
            //else if (IOrderType == 2)
            //{
            //    this.Text = "待归还工单列表";
            //}
            //else if (IOrderType == 3)
            //{
            //    this.Text = "已完成工单列表";
            //}
        }

        // 加载工具列表
        private void _LoadGrid()
        {
            try
            {
                gc_main.DataSource = ToolDataMgr.GetInstance().GetOrderList(-1);
                gridView1.ExpandAllGroups();
            }
            catch
            {
                gc_main.DataSource = null;
                gc_main.Refresh();
            }
        }

        private void Tool_System_OrderList_Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }

        private void tv_order_ItemCustomize(object sender, DevExpress.XtraGrid.Views.Tile.TileViewItemCustomizeEventArgs e)
        {
            //var task = tileView.GetRow(e.RowHandle) as TaskRecord;
            //DataRow dataRow = tv_order.GetDataRow(e.RowHandle);
            //if (dataRow != null)
            //{
            //    //e.Item["f_id"].Text = dataRow["f_id"].ToString();

            //    //e.Item.AppearanceItem.Normal.BackColor = Color.LightGray;
            //    //e.Item.AppearanceItem.Normal.ForeColor = Color.DarkGray;
            //}
        }

        private void cardView1_CustomDrawCardCaption(object sender, DevExpress.XtraGrid.Views.Card.CardCaptionCustomDrawEventArgs e)
        {
            //DataRow dataRow = gridView1.GetDataRow(e.RowHandle) as DataRow;
            //if (dataRow != null)
            //    e.CardCaption = "工单序号:" + dataRow["f_id"].ToString();
        }

        private void gridView1_MouseDown_1(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = gridView1.CalcHitInfo(new Point(e.X, e.Y));
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                //判断光标是否在行范围内 
                if (hInfo.InRow && !hInfo.InGroupRow)
                {
                    DataRow vCurrenRow = gridView1.GetFocusedDataRow();

                    Tool_System_OrderDetail_Form oForm = new Tool_System_OrderDetail_Form();
                    oForm.m_oOrder.IID = (int)vCurrenRow["f_id"];
                    oForm.m_oOrder.IProposID = GetRowInt(vCurrenRow, "f_proposid");
                    oForm.m_oOrder.SProposName = GetRowString(vCurrenRow, "f_proposname");
                    oForm.m_oOrder.SProposTime = (DateTime)vCurrenRow["f_propostime"];
                    oForm.m_oOrder.ISendID = GetRowInt(vCurrenRow, "f_sendid");
                    oForm.m_oOrder.SSendName = GetRowString(vCurrenRow, "f_sendname");
                    oForm.m_oOrder.SSendTime = GetRowDatetime(vCurrenRow, "f_sendtime");
                    oForm.m_oOrder.IReciveID = GetRowInt(vCurrenRow, "f_reciveid");
                    oForm.m_oOrder.SReciveName = GetRowString(vCurrenRow, "f_recivename");
                    oForm.m_oOrder.SReciveTime = GetRowDatetime(vCurrenRow, "f_recivetime");
                    oForm.m_oOrder.IStatus = (OrderStatus)vCurrenRow["f_status"];
                    
                    m_bRefresh = false;
                    if (oForm.ShowDialog() == DialogResult.OK)
                    {
                        _LoadGrid();
                    }
                    m_bRefresh = true;
                }
            }
        }

        private int GetRowInt(DataRow dataRow, string sColName)
        {
            try
            {
                return (int)dataRow[sColName];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return -1;
        }

        private string GetRowString(DataRow dataRow, string sColName)
        {
            try
            {
                return dataRow[sColName].ToString();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return "";
        }

        private DateTime GetRowDatetime(DataRow dataRow, string sColName)
        {
            try
            {
                return (DateTime)dataRow[sColName];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return DateTime.MinValue;
        }

        private void gridView1_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
        {
            GridGroupRowInfo gridGroupRowInfo = e.Info as GridGroupRowInfo;
            GridView gridview = sender as GridView;
            int index = gridview.GetDataRowHandleByGroupRowHandle(e.RowHandle);

            int iStatus = int.Parse(gridview.GetRowCellValue(index, "f_status").ToString());
            gridGroupRowInfo.GroupText = Utility.GetOrderStateString((OrderStatus)iStatus) + "订单";
        }

        private void onElapsedEventHandler(object sender, ElapsedEventArgs e)
        {
            if (m_bRefresh)
                _LoadGrid();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (m_bRefresh && this.IsActive)
                _LoadGrid();
        }

    }
}